package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.bjesc_b2c.app.appraiset.widget.popupwindow.BasicDropDownPop;
import com.rxretrofit_build.CarPhotoID;
import com.rxretrofit_build.util.MapUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import master_bean.CarBigPhotoStatusBean;
import rxretrofit_base.model.KeyBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by lenovo on 2017/2/27.
 * 配置
 */

public class DeviceActivity extends BaseActivity implements View.OnClickListener {
    @InjectView(R.id.tv_central_control)
    TextView centralControlTv;
    @InjectView(R.id.tv_car_window_control)
    TextView carWindowControlTv;
    @InjectView(R.id.tv_car_ceiling_control)
    TextView carCeilingControlTv;
    @InjectView(R.id.tv_electric_reflector)
    TextView electricReflectorTv;
    @InjectView(R.id.tv_electric_chair)
    TextView electricChairTv;
    @InjectView(R.id.tv_entertainment)
    TextView entertainmentTv;
    @InjectView(R.id.tv_air_conditioner)
    TextView airConditionerTv;
    @InjectView(R.id.tv_car_lamp)
    TextView carLampTv;
    @InjectView(R.id.tv_air_bag)
    TextView airBagTv;
    @InjectView(R.id.tv_dashboard)
    TextView dashboardTv;
    @InjectView(R.id.tv_turn_control)
    TextView turnControlTv;
    @InjectView(R.id.tv_starter)
    TextView starterTv;
    @InjectView(R.id.tv_cooling_system)
    TextView coolingSystemTv;
    @InjectView(R.id.tv_exhaust_system)
    TextView exhaustSystemTv;
    @InjectView(R.id.tv_drive)
    TextView driveTv;
    @InjectView(R.id.tv_arrester)
    TextView arresterTv;
    @InjectView(R.id.tv_gearbox)
    TextView gearboxTv;
    @InjectView(R.id.tv_engine)
    TextView engineTv;
    @InjectView(R.id.btn_previous_page)
    Button previousPageTv;
    @InjectView(R.id.btn_next_page)
    Button nextPageTv;
    private BasicDropDownPop basicPop;
    private final int startRequest=88;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device);
        ButterKnife.inject(this);
        basicPop = new BasicDropDownPop(this,this);

        getSurfaceStatus();
    }

    @OnClick({R.id.iv_return, R.id.tv_save_info, R.id.tv_central_control, R.id.tv_car_window_control,
            R.id.tv_car_ceiling_control, R.id.tv_electric_reflector, R.id.tv_electric_chair, R.id.tv_entertainment,
            R.id.tv_air_conditioner, R.id.tv_car_lamp, R.id.tv_air_bag, R.id.tv_dashboard, R.id.tv_turn_control,
            R.id.tv_starter, R.id.tv_cooling_system, R.id.tv_exhaust_system, R.id.tv_drive, R.id.tv_arrester,
            R.id.tv_gearbox, R.id.tv_engine, R.id.btn_previous_page, R.id.btn_next_page,R.id.rlty_title})
    public void onClick(View view) {
        Intent intent = new Intent(DeviceActivity.this, CarrosserieDescribeActivity.class);
        Intent radioIntent = new Intent(DeviceActivity.this, ComplexComponentsDetailActivity.class);
        List<String> light = null;
        List<String> moderate = null;
        List<String> seriously = null;
        switch (view.getId()) {
            case R.id.iv_return:        //返回
                finish();
                break;
            case R.id.tv_save_info:     //保存        //取消了
                break;
            case R.id.tv_central_control:   //中控装置
                buildIntent(intent, true, "按钮失灵", "按钮破损", "", "", "中控装置", "", CarPhotoID.CarDeviceID.centralControl);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_car_window_control:    //车窗装置
                buildIntent(intent, true, "异响", "升降故障", "", "", "车窗装置", "", CarPhotoID.CarDeviceID.windowControl);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_car_ceiling_control:       //天窗装置
                buildIntent(intent, true, "异响", "升降故障", "", "", "天窗装置", "", CarPhotoID.CarDeviceID.ceilingControl);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_electric_reflector:        //电动反光镜
                buildIntent(intent, true, "异响", "故障", "", "", "电动反光镜", "", CarPhotoID.CarDeviceID.electricReflector);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_electric_chair:        //电动座椅
                buildIntent(intent, true, "故障", "", "", "", "电动座椅", "", CarPhotoID.CarDeviceID.electricChair);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_entertainment:     //娱乐设备
                buildIntent(intent, true, "音频故障", "视频故障", "", "", "娱乐设备", "", CarPhotoID.CarDeviceID.entertainment);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_air_conditioner:       //空调
                buildIntent(intent, true, "压缩机故障", "鼓风机故障", "", "", "空调", "", CarPhotoID.CarDeviceID.airConditioner);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_car_lamp:          //车灯
                buildIntent(intent, true, "转向灯故障", "大灯故障", "阅读灯故障", "雾灯故障", "车灯", "", CarPhotoID.CarDeviceID.carLamp);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_air_bag:       //气囊
                buildIntent(intent, true, "弹射", "", "", "", "气囊", "", CarPhotoID.CarDeviceID.airBag);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_dashboard:     //仪表类
                buildIntent(intent, true, "故障灯", "仪表指正故障", "", "", "仪表类", "", CarPhotoID.CarDeviceID.dashboard);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_turn_control:      //转向装置
                buildIntent(intent, true, "异响", "漏油", "", "", "转向装置", "", CarPhotoID.CarDeviceID.turnControl);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_starter:           //启动机
                buildIntent(intent, true, "异响", "启动异常", "", "", "启动机", "", CarPhotoID.CarDeviceID.starter);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_cooling_system:        //冷却系统
                buildIntent(intent, true, "水箱曾修复", "水箱曾更换", "", "", "冷却系统", "", CarPhotoID.CarDeviceID.coolingSystem);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_exhaust_system:        //排气系统
                buildIntent(intent, true, "异响", "尾气异常", "", "", "排气系统", "", CarPhotoID.CarDeviceID.exhaustSystem);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_drive:         //驱动器
                buildIntent(intent, true, "漏油", "异响", "", "", "驱动器", "", CarPhotoID.CarDeviceID.drive);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_arrester:      //制动器
                buildIntent(intent, true, "异响", "", "", "", "制动器", "", CarPhotoID.CarDeviceID.arrester);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_gearbox:       //变速箱
                light = getLight("漏油", "拆卸打胶");
                moderate = getLight("闯档", "跳档");
                seriously = getLight("无法换档", "需大修");
                buildRadioIntent(radioIntent, "变速箱", light, moderate, seriously, CarPhotoID.CarDeviceID.gearbox);
                startIntent(radioIntent, startRequest);
                break;
            case R.id.tv_engine:        //发动机
                light = getLight("渗漏油", "气门响", "更换缸垫","皮带响","油底壳更换过");
                moderate = getLight("严重漏油", "油水混", "烧机油","发动机缸体更换过","异响严重");
                seriously = getLight("缺缸", "抖动严重","异响严重");
                buildRadioIntent(radioIntent, "发动机", light, moderate, seriously, CarPhotoID.CarDeviceID.engine);
                startIntent(radioIntent, startRequest);
                break;
            case R.id.btn_previous_page:        //上一页到内饰
                startIntent(new Intent(DeviceActivity.this,CarOrnamentsItemActivity.class),true);
                break;
            case R.id.btn_next_page:        //下一页到综合评价
                startIntent(new Intent(DeviceActivity.this,ComprehensiveEvaluationActivity.class),true);
                break;
            case R.id.rlty_car_form:        //车源图片
                startIntent(new Intent(DeviceActivity.this, CarSourceActivity.class),true);
                break;
            case R.id.rlty_car_Framework:   //骨架
                startIntent(new Intent(DeviceActivity.this, CarFrameworkImageActivity.class),true);
                break;
            case R.id.rlty_car_appearance:  //外观
                startIntent(new Intent(DeviceActivity.this,CarSurfaceItemActivity.class),true);
                break;
            case R.id.rlty_car_interior:    //内饰
                startIntent(new Intent(DeviceActivity.this, CarOrnamentsItemActivity.class),true);
                break;
            case R.id.rlty_car_damage:      //配置
            basicPop.dismiss();
                break;
            case R.id.rlty_car_comprehensive:   //综合评估
                startIntent(new Intent(DeviceActivity.this, ComprehensiveEvaluationActivity.class),true);
                break;
            case R.id.rlty_car_photo:       //合影
                startIntent(new Intent(DeviceActivity.this, PhotographsActivity.class),true);
                break;
            case R.id.rlty_title:
                basicPop.showAsDropDown(findViewById(R.id.rlty_title));
                break;
        }
    }


    /**
     * 创建单选项的内容的集合
     *
     * @param strings
     * @return
     */
    private List<String> getLight(String... strings) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < strings.length; i++) {
            list.add(strings[i]);
        }
        return list;
    }

    /**
     * 创建跳转单选的Intent
     *
     * @param radioIntent
     * @param title
     * @param light
     * @param moderate
     * @param seriously
     * @param sourceID
     */
    private void buildRadioIntent(Intent radioIntent, String title, List<String> light, List<String> moderate, List<String> seriously, String sourceID) {
        radioIntent.putExtra("title", title);
        radioIntent.putStringArrayListExtra("light", (ArrayList<String>) light);
        radioIntent.putStringArrayListExtra("moderate", (ArrayList<String>) moderate);
        radioIntent.putStringArrayListExtra("seriously", (ArrayList<String>) seriously);
        radioIntent.putExtra("sourceID", sourceID);
    }

    /**
     * 跳转页面需要带的参数
     *
     * @param intent
     * @param isScore        是否有选择项
     * @param scoreOneName   选项一的名称
     * @param scoreTwoName   选项二的名称
     * @param scoreThreeName 选项三的名称
     * @param scoreFourName  选项四的名称
     * @param title          标题
     * @param scoreTitle     选项的标题
     * @param sourceID       图片ID
     */
    private void buildIntent(Intent intent, boolean isScore, String scoreOneName,
                             String scoreTwoName, String scoreThreeName, String scoreFourName,
                             String title, String scoreTitle, String sourceID) {
        intent.putExtra("isScore", isScore);
        intent.putExtra("scoreOneName", scoreOneName);
        intent.putExtra("scoreTwoName", scoreTwoName);
        intent.putExtra("scoreThreeName", scoreThreeName);
        intent.putExtra("scoreFourName", scoreFourName);
        intent.putExtra("title", title);
        intent.putExtra("scoreTitle", scoreTitle);
        intent.putExtra("sourceID", sourceID);
        intent.putExtra("classID", CarPhotoID.deviceID);
    }

    private void getSurfaceStatus() {
        HashMap<String, Object> build = new MapUtil()
                .put("id",  CustomSharedPreferences.getString(this, KeyBean.CARID.valueOf(),KeyBean.FILE_NAME.valueOf()))
                .put("class_id", CarPhotoID.deviceID)
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(this, build).create(IApiActivityRequest.class).getFrameworkStatus(),
                new BaseSubscriber<TokenBean<List<CarBigPhotoStatusBean>>>(this) {
                    @Override
                    public void onSuccess(TokenBean<List<CarBigPhotoStatusBean>> o) {
                        if (o.getData() != null && o.getData().size() > 0) {
                            for (int i = 0; i < o.getData().size(); i++) {
                                judgmentStatus(o.getData().get(i).getAttr_id(), true);
                            }

                        }
                    }
                    @Override
                    public void onFailure(Throwable e) {

                    }
                });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == startRequest) {
            if (data != null) {
                judgmentStatus(data.getStringExtra("sourceID"), data.getBooleanExtra("isChange", false));
            }
        }
    }
    /**
     * 判断哪些控件需要改变状态
     *
     * @param status 需要改变的ID
     * @param change true 是设置状态，false 是取消状态
     */
    private void judgmentStatus(String status, boolean change) {
        if(TextUtils.isEmpty(status)){
            return;
        }
        switch (status) {
            case CarPhotoID.CarDeviceID.centralControl:   //中控装置
                setViewStatus(change, centralControlTv);
                break;
            case CarPhotoID.CarDeviceID.windowControl:    //车窗装置
                setViewStatus(change, carWindowControlTv);
                break;
            case CarPhotoID.CarDeviceID.ceilingControl:       //天窗装置
                setViewStatus(change, carCeilingControlTv);
                break;
            case CarPhotoID.CarDeviceID.electricReflector:        //电动反光镜
                setViewStatus(change, electricReflectorTv);
                break;
            case CarPhotoID.CarDeviceID.electricChair:        //电动座椅
                setViewStatus(change, electricChairTv);
                break;
            case CarPhotoID.CarDeviceID.entertainment:     //娱乐设备
                setViewStatus(change, entertainmentTv);
                break;
            case CarPhotoID.CarDeviceID.airConditioner:       //空调
                setViewStatus(change, airConditionerTv);
                break;
            case CarPhotoID.CarDeviceID.carLamp:          //车灯
                setViewStatus(change, carLampTv);
                break;
            case CarPhotoID.CarDeviceID.airBag:       //气囊
                setViewStatus(change, airBagTv);
                break;
            case CarPhotoID.CarDeviceID.dashboard:     //仪表类
                setViewStatus(change, dashboardTv);
                break;
            case CarPhotoID.CarDeviceID.turnControl:      //转向装置
                setViewStatus(change, turnControlTv);
                break;
            case CarPhotoID.CarDeviceID.starter:           //启动机
                setViewStatus(change, starterTv);
                break;
            case CarPhotoID.CarDeviceID.coolingSystem:        //冷却系统
                setViewStatus(change, coolingSystemTv);
                break;
            case CarPhotoID.CarDeviceID.exhaustSystem:        //排气系统
                setViewStatus(change, exhaustSystemTv);
                break;
            case CarPhotoID.CarDeviceID.drive:         //驱动器
                setViewStatus(change, driveTv);
                break;
            case CarPhotoID.CarDeviceID.arrester:      //制动器
                setViewStatus(change, arresterTv);
                break;
            case CarPhotoID.CarDeviceID.gearbox:       //变速箱
                setViewStatus(change, gearboxTv);
                break;
            case CarPhotoID.CarDeviceID.engine:        //发动机
                setViewStatus(change, engineTv);
                break;
        }
    }

    /**
     * 设置控件状态
     *
     * @param change
     * @param view
     */
    private void setViewStatus(boolean change, TextView view) {
        if (change) {
            changeYellowFrame(view);
        } else {
            changeWhiteFrame(view); //取消状态
        }
    }

    /**
     * 取消状态
     *
     * @param v
     */
    private void changeWhiteFrame(TextView v) {
        v.setTextColor(getResources().getColor(R.color.colorFF030303));
        v.setBackgroundResource(R.drawable.shape_text_frame_bbbbbb);
    }

    private void changeYellowFrame(TextView v) {
        v.setBackgroundResource(R.drawable.shape_text_frame_yellow);
        v.setTextColor(getResources().getColor(R.color.colorWhite));
    }

}
